import urllib2

DEBUG = True
TAG = 'UTIL'

def d_print(tag, vars=(), namespace={}, msg=""):
	global DEBUG
	if vars and not namespace and not msg:
		msg = vars
	if DEBUG and vars and namespace:
		for var in vars:
			print "[" + tag + "]:\033[0;31;48m%s\033[0m" % var,
			print namespace[var]
	if DEBUG and msg:
		print "[" + tag + "]:\033[0;31;48m%s\033[0m" % msg,

def http_send(url, body="", exheaders="", login=False):
	headers = { 'User-Agent':'IIC2.0/PC 3.2.0540',}
	headers.update(exheaders)
	request = urllib2.Request(url,headers=headers,data=body) #if body is not "" then POST will be the HTTP request
	try:
		conn = urllib2.urlopen(request)
	except urllib2.HTTPError, e:
		code = e.getcode()
		print locals().get('code')
		if code == 401 or code == 404:
			if login:
				raise FetionAuthError(code, msg)
		return None
	except urllib2.URLError, e:
		reason =  e.reason
		d_print(TAG, msg = reason)
		return None
	return conn
